package com.drguo.higouguo.web;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import com.drguo.higouguo.entity.Category;
import com.drguo.higouguo.entity.User;
import com.drguo.higouguo.service.CategoryService;
import com.drguo.higouguo.service.UserService;
import com.opensymphony.xwork2.ActionContext;

public class UserAction {
	private UserService userService;
	private User user;
	private File avatar;
	private String avatarFileName;
	private CategoryService categoryService;
	private List<Category> categories;
	
	//注册
	public String reg() {
		userService.register(user);
		return "regsuc";
	}

	// 唯一性验证
	public void isExist() {
		boolean isExist = userService.isExist(user.getName());
		try {
			PrintWriter w = ServletActionContext.getResponse().getWriter();
			w.print(isExist);
			w.flush();
			w.close();
		} catch (IOException e) {

			e.printStackTrace();
		}
	}

	// 普通用户登录
	public String login() {
		User u = userService.login(user.getName(), user.getPassword());
		if (u == null) {
			return "loginfail";
		} else {
			Map<String, Object> m = ActionContext.getContext().getSession();
			m.put("user", u);
			categories = categoryService.getCategoriesForIndex();
			// System.out.println("-----------------------"+ActionContext.getContext().getSession());
			return "loginsuc";
		}
	}

	// 管理员登录
	private String captcha;
	private String msg;

	public String adminLogin() {
		String vcode = ActionContext.getContext().getSession().get("vcode")
				.toString();
		User u = userService.login(user.getName(), user.getPassword());
//		System.out.println("----------------------------------------------"+u.getRole());
		// 判断用户名与密码
		if (u == null) {
			msg = "用户名或密码不正确！";
			return "adminlogin";
		}
		// 判断权限
		if (!u.getRole().equals("a")) {
			msg = "权限不足！";
			return "adminlogin";
		}
		// 判断验证码
		if (!vcode.equals(captcha)) {
			msg = "您输入的验证码有误！";
			return "adminlogin";
		}
		Map<String, Object> m = ActionContext.getContext().getSession();
		m.put("user", u);
		return "adminindex";
	}

	// 在header.jsp里，退出登录
	public String logout() {
		ActionContext.getContext().getSession().clear();
		return "login";
	}

	// 在header.jsp里，登录后点击进入我的账户
	public String showUsercenter() {
		/*System.out.println("-----------------------"
				+ ActionContext.getContext().getSession());*/
		User user = (User) ActionContext.getContext().getSession().get("user");

		if (user == null) {
			return "login";
		} else {
			return "usercenter";
		}

	}

	// 更新头像
	public String updateAvatar() {
		String userId = ((User) ActionContext.getContext().getSession()
				.get("user")).getId();
		//图片存放路径
		String dir = ServletActionContext.getServletContext().getRealPath(
				"/useravatars");
		String suffix = "";
		if (avatar != null) {
			suffix = avatarFileName.substring(avatarFileName.lastIndexOf("."));
			File saveFile = new File(new File(dir), userId + suffix);
			if (!saveFile.getParentFile().exists()) {
				saveFile.getParentFile().mkdir();
			}
			try {
				FileUtils.copyFile(avatar, saveFile);
			} catch (IOException e) {

				e.printStackTrace();
			}
		}
		userService.updateAvatar(userId, "useravatars/" + userId + suffix);
		((User) ActionContext.getContext().getSession().get("user"))
				.setAvatar("useravatars/" + userId + suffix);
		return "usercenter";
	}

	public File getAvatar() {
		return avatar;
	}

	public void setAvatar(File avatar) {
		this.avatar = avatar;
	}

	public String getAvatarFileName() {
		return avatarFileName;
	}

	public void setAvatarFileName(String avatarFileName) {
		this.avatarFileName = avatarFileName;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public String getCaptcha() {
		return captcha;
	}

	public void setCaptcha(String captcha) {
		this.captcha = captcha;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public CategoryService getCategoryService() {
		return categoryService;
	}

	public void setCategoryService(CategoryService categoryService) {
		this.categoryService = categoryService;
	}

	public List<Category> getCategories() {
		return categories;
	}

	public void setCategories(List<Category> categories) {
		this.categories = categories;
	}
}
